<link rel="stylesheet" type="text/css" href="tag/selectpage.css" />
<link rel="stylesheet" type="text/css" href="tag/demo.css" />
<section id="main-content">
	<section class="wrapper">
		<!--state overview end-->
		<div class="row">
			<div class="col-lg-12">
				<section class="panel">
					<header class="panel-heading">
						添加权限
					</header>
					<form>
						<div class="panel-body">
							<div class="form-group customerForm">
								<label class="col-sm-2 control-label">公司*</label>
								<div class="col-sm-10" id="companys" name="companys">
									<div class="row">
										<span ng-repeat="x in comp">
											<div class="col-sm-2" style="line-height: normal;">
												<div class="checkbox">
													<label>
														<input type="checkbox" value="{{x.companyId}}" onclick="getDept(this)"> {{x.company}}
													</label>
												</div>
											</div>
										</span>
									</div>
								</div>
							</div>
							<div class="form-group customerForm" id="departments">
								<label class="col-sm-2 control-label">部门*</label>
								<div class="col-sm-10">
									<span class="doprocheck" style="line-height: normal;">
										<div class="checkbox">
											<label>
												<input type="checkbox" id="checkAll" value="ALL"> 全部
											</label>
										</div>
									</span>
								</div>
							</div>

							<div class="form-group customerForm">
								<label class="col-sm-2 control-label">菜单</label>
								<div class="col-sm-10">
									<select class="form-control" certus-required name="level1" id="level1" onchange="change(this.value)">
										<option value="XXZX">信息中心</option>
										<option value="YYGL">运营管理</option>
										<option value="CGLC">物资管理</option>
										<option value="XZZX">行政中心</option>
										<option value="XTXX">系统管理</option>
										<option value="WDLC">我的流程</option>
										<option value="WDXM">我的项目</option>
									</select>
								</div>
							</div>
							<div class="form-group customerForm">
								<label class="col-sm-2 control-label">功能</label>
								<div class="col-sm-10">
									<select class="form-control" name="authorityCode" id="authorityCode">
									</select>
								</div>
							</div>
							<!--<div class="form-group customerForm">-->
							<!--<label class="col-sm-2 control-label">类型</label>-->
							<!--<div class="col-sm-10">-->
							<!--<select class="form-control" certus-required name="type" id="type">-->
							<!--<option value="0">功能</option>-->
							<!--<option value="1">菜单</option>-->
							<!--</select>-->
							<!--</div>-->
							<!--</div>-->
							<div class="form-group customerForm">
								<label class="col-sm-2 control-label">人员</label>
								<div class="col-sm-10">
									<input type="text" placeholder="输入后请选择" class="input-block-level" id="selectpage">
								</div>
							</div>
							<div class="form-group customerForm">
								<label class="col-sm-2 control-label">权限</label>
								<div class="col-sm-10" id="authority">
									<div class="row">
										<div class="col-sm-3" style="line-height: normal;">
											<div class="checkbox">
												<label>
													<input type="checkbox" checked value="C"> 增加
												</label>
											</div>
										</div>
										<div class="col-sm-3" style="line-height: normal;">
											<div class="checkbox">
												<label>
													<input type="checkbox" checked value="D"> 删除
												</label>
											</div>
										</div>
										<div class="col-sm-3" style="line-height: normal;">
											<div class="checkbox">
												<label>
													<input type="checkbox" checked value="U"> 修改
												</label>
											</div>
										</div>
										<div class="col-sm-3" style="line-height: normal;">
											<div class="checkbox">
												<label>
													<input type="checkbox" checked value="R"> 查询
												</label>
											</div>
										</div>
									</div>
								</div>
							</div>
							<div class="form-group customerForm">
								<label class="col-sm-2 control-label">说明</label>
								<div class="col-sm-10">
									<textarea class="form-control" id="comments" name="comments" maxlength="120"></textarea>
								</div>
							</div>

						</div>
					</form>
				</section>
				<section class="panel">
					<div class="panel-body">
						<p class="default-buttons">
							<a href="javascript:void(0);" class="button-back btn btn-info" onclick="window.history.back()">返回</a>
							<a href="javascript:void(0);" class="button-next  btn btn-success submit">确认提交</a>
						</p>
					</div>
				</section>
			</div>
		</div>
	</section>
</section>
<script type="text/javascript" src="js/autocomplete/jquery.autocomplete.js"></script>
<script src="tag/b.selectpage.js"></script>
<script src="tag/selectpage.js"></script>
<script>
	//全选反选
	$("#checkAll").click(function () {
		if (this.checked) {
			$("#departments").find("input[type='checkbox']").each(function (i, n) {
				if ($(n).val() != "ALL") {
					$(n).attr("checked", true)
				}
			});
		} else {
			$("#departments").find("input[type='checkbox']").each(function (i, n) {
				if ($(n).val() != "ALL") {
					$(n).attr("checked", false)
				}
			});
		}
	});

	function getdeptName(deptCode) {
		var content = "";
		if (deptCode == "HFKJ") {
			content = "浩方科技";
		} else if (deptCode == "HFXX") {
			content = "浩方信息";
		} else if (deptCode == "STSBJ") {
			content = "赛特斯北京";
		} else if (deptCode == "STSGD") {
			content = "赛特斯广东";
		} else if (deptCode == "STSJT") {
			content = "赛特斯集团";
		} else if (deptCode == "STSNJ") {
			content = "赛特斯南京";
		} else if (deptCode == "STSSH") {
			content = "赛特斯上海";
		} else if (deptCode == "SYB-RX") {
            content = "融合运维事业部";
        }else if (deptCode == "HFXXBJ") {
            content = "浩方信息北京";
        }else if (deptCode == "HFXXNJ") {
            content = "浩方信息南京";
        }else if (deptCode == "HFXXSH") {
            content = "浩方信息上海";
        };
		return content;
	}

	function addrole($scope, CertusFormAjax) {
		CertusFormAjax.ajax("companyListByViewCode", {}, function (response) {
			$scope.comp = JSON.parse(response.data);
		});

		CertusFormAjax.ajax("findSpUserList", {}, function (response) {
			$('#selectpage').bSelectPage({
				showField: 'userName',
				keyField: 'userId',
				data: response.data,
				multiple: true
			});
		});

		this.getDept = function (checkbox) {
			if (checkbox.checked) {
				var html = '<hr/><div class="col-sm-2" id="' + $(checkbox).val() + '">' + getdeptName($(checkbox).val()) + '：</div><div class="col-sm-10" style="min-height: 34px;"><div class="lis_check">';
				CertusFormAjax.ajax("deptedList", { "companyCode": $(checkbox).val() }, function (response) {
					var data = JSON.parse(response.data);
					for (var i = 0; i < data.length; i++) {
						html += '<div class="lis_input"><div class="checkbox"><label><input type="checkbox"' +
							' onclick="checkonces(this)" value="' + data[i].departmentCode + '">  ' + data[i].departmentName + '</label></div></div>';
					}
					html += '</div></div>';
					$("#departments").append(html);
					$("#checkAll").attr("checked", false);
				});
			} else {
				$("#" + $(checkbox).val()).next().remove();
				$("#" + $(checkbox).val()).prev().remove();
				$("#" + $(checkbox).val()).remove();

				var flag = true;
				$("#departments").find("input[type='checkbox']").each(function (i, n) {
					if ($(n).val() != "ALL") {
						if (!n.checked) {
							flag = false;
						}
					}
				});
				if (flag) {
					$("#checkAll").attr("checked", true);
				} else {
					$("#checkAll").attr("checked", false);
				}
			}
		};

		//全选反选
		this.checkonces = function (oncesdept) {
			if (oncesdept.checked) {
				var flag = true;
				$("#departments").find("input[type='checkbox']").each(function (i, n) {
					if ($(n).val() != "ALL") {
						if (!n.checked) {
							flag = false;
						}
					}
				});
				if (flag) {
					$("#checkAll").attr("checked", true);
				} else {
					$("#checkAll").attr("checked", false);
				}
			} else {
				$("#checkAll").attr("checked", false);
			}
		};

		$(".submit").on('click', function () {
			var arr = $("form").serializeArray();
			var data = {};
			for (var i = 0; i < arr.length; i++) {
				data[arr[i].name] = arr[i].value;
			};
			var viewCompany = "";
			$("#companys").find("input[type='checkbox']:checked").each(function (i, n) {
				if (i == 0) {
					viewCompany += $(n).val();
				} else {
					viewCompany += "," + $(n).val();
				}
			});
			if (viewCompany == "") {
				layer.msg("必须选择一个公司!");
				return;
			}
			data.viewCompany = viewCompany;
			var deptCodes = "";
			$("#departments").find("input[type='checkbox']:checked").each(function (i, n) {
				if ($(n).val() != "ALL") {
					if (i == 0) {
						deptCodes += $(n).val();
					} else {
						deptCodes += "," + $(n).val();
					}
				} else {
					deptCodes = "ALL";
					return false;
				}
			});

			data.deptCodes = deptCodes;
			if (deptCodes == "") {
				layer.msg("必须选择一个部门!");
				return;
			}

			var authority = "";
			$("#authority").find("input[type='checkbox']:checked").each(function (i, n) {
				authority += $(n).val();
			});
			if (authority == "") {
				layer.msg("必须选择一个权限!");
				return;
			}
			data.authority = authority;

			data.level1Name = $('#level1 option:selected').text();
			data.authorityName = $('#authorityCode option:selected').text();
			if ($("#selectpage").val() == "") {
				layer.msg("请输入人员!");
				return;
			}
			data.userIds = $("#selectpage").val();
			CertusFormAjax.ajax('addRole', { "param": JSON.stringify(data) }, function () {
				layer.msg("添加成功！");
				window.history.back();
			});
		});
	};

	function change(menu) {
		var selectid = document.getElementById("authorityCode");
		$(selectid).empty();
		if (menu == "YYGL") {
			selectid.options.add(new Option("项目查询", "XMCX"));
			selectid.options.add(new Option("项目进展", "XMJZ"));
			selectid.options.add(new Option("客户列表", "KHLB"));
			selectid.options.add(new Option("合同管理", "HTGL"));
            selectid.options.add(new Option("工时报表", "GSBB"));
		} else if (menu == "CGLC") {
			selectid.options.add(new Option("资产管理", "XMZCGL"));
			selectid.options.add(new Option("物资管理", "WZGL"));
            selectid.options.add(new Option("资产历史", "ZCLS"));
		} else if (menu == "XZZX") {
			selectid.options.add(new Option("员工列表", "YGLB"));
			selectid.options.add(new Option("入职管理", "XYGRZ"));
			selectid.options.add(new Option("离职管理", "LZGL"));
			selectid.options.add(new Option("座位管理", "ZWGL"));
            selectid.options.add(new Option("员工归集", "YRCB"));
            selectid.options.add(new Option("成本设置", "CBSZ"));
            selectid.options.add(new Option("归集结果", "GJJG"));
		} else if (menu == "XXZX") {
			selectid.options.add(new Option("人员查询", "RYCZQX"));
		} else if (menu == "XTXX") {
			selectid.options.add(new Option("权限管理", "QXGL"));
			selectid.options.add(new Option("系統角色", "XTJS"));
		} else if (menu == "WDLC") {
			selectid.options.add(new Option("公章使用", "GZSY"));
		} else if (menu == "WDXM") {
            selectid.options.add(new Option("立项申请", "LXSQ"));
        }
	};
	change("XXZX");
</script>